<?php
require_once 'Connection.php';

/**
 * class nhanvien
 *
 * MaNV, HoTen, DiaChi, NgaySinh, Phai, Luong, Phong
 *
 * @author: hqlam
*/
class nhanvien {

	/**
	 * Select nhanvien
	 *
	 * @return array of nhanvien
	 */
	public static function Select($filters) {
		$con = Connection::GetConnection();

		$query = "SELECT MaNV, HoTen, DATE_FORMAT(NgaySinh, '%d/%m/%Y') AS NgaySinh, DiaChi, Phai, Luong, Phong FROM nhanvien";

		$fields = array('HoTen', 'DiaChi', 'NgaySinh', 'Luong', 'Phai');
		$where = '';

		// prepare filter
		if (count($filters) > 0) {
			foreach ($filters as $key => $value) {
				if (!in_array($key, $fields)) continue;

				if ($key == 'Phai') {
					$where .= "$key = $value";
				} else if ($key == 'NgaySinh') {
					$where .= "DATE_FORMAT($key, '%d/%m/%Y') = '$value'";
				} else {
					$where .= "$key LIKE '%$value%'";
				}
				
				$where .= " AND ";
			}
		}
		
		if ($where != '') {
			$query = preg_replace('/ AND $/', '', "$query WHERE $where");
		}
		
		$lst = mysql_query($query, $con);
		$result = array();
		while ($item = mysql_fetch_assoc($lst))
			array_push($result, $item);
		return $result;
	}
	
}

?>